package com.lcw.lostandfound.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.util.Date;


/**
 * @author lcw
 */
@Component
public class AliyunUploadDown {

    public static String picOSS( MultipartFile uploadFile) throws Exception {
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String bucket="https://lcw2.oss-cn-beijing.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录
        // https://ram.console.aliyun.com 创建
        String accessKeyId = "LTAI4GFSRRxZUpALjurH1hLG";
        String accessKeySecret = "ejXSQqM9u5y7tq6o3A1jyqf2BDWFlK";
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        // 上传
        long time = new Date().getTime();
        //设置响应头
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentType("image/jpg");
	    ossClient.putObject("lcw2", uploadFile.getName()+time+".jpg", new ByteArrayInputStream(uploadFile.getBytes()),meta);
        // 关闭client
        ossClient.shutdown();
//        Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
//        String url = ossClient.generatePresignedUrl("houzeyu", uploadFile.getName()+".jpg", expiration).toString();
         String url=bucket+"/"+uploadFile.getName()+time+".jpg";
        System.out.println(url);
        return url;
    }
}
